import operate from '@dfeidao/atom-nodejs/db/operate';
import log from '@dfeidao/atom-nodejs/logger/log';
import doc_code from '@dfeidao/atom-nodejs/msg/doc-code';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	cookie: {
		uk: string;
		[key: string]: string
	};
	urls: {
		base: string;
		origin: string;
		url: string;
	};
	query: {};
	params: {};
	headers: {};
	captcha: string;
	data: {
		equip_type_name: string;
		equip_type_no: string;
		equip_internal_no: string;
		equip_name: string;
		fixed_assets_type: string;
		equipment_state: string;
		transfer_time: string;
		transfer_price: string;
		transfer_date: number;
		// address: string;
	};
	hidden_info: Array<{ hidden_source: string }>;
	hidden_data: Array<{
		hidden_source: string,
		hidden_name: string,
		assess_level: string
	}>;
	design_data: Array<{
		attr_no: string;
		attr_name: string;
		attr_value: string;
	}>;
}
// 标题	设备自定义属性equ_user_defined_attribute
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// productid	产品ID	string	50	√	ⅹ
// productid	设备编号	string	32	√	ⅹ
// attr_no	属性编号	string	32	√	ⅹ
// attr_name	属性名称	string	32	√	ⅹ
// attr_value	属性值	string	32	√	ⅹ

// 'hazard_source', ['hidden_source', 'hidden_name', 'assess_level']
interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:e:\考核-2\kh\src\device-management-ygh\zj-000009\s010,action_id:' + action_id);
	const res = await doc_code(action_id, session_id, systemid,
		'BM0013887', '', 1);// result和results在编码结果为1时一样；
	const { insert, exec } = operate(action_id, session_id, systemid);

	insert('equipment_facilities', {
		equip_no: res.result,
		// equip_internal_no:,
		equip_type_no: message.data.equip_type_no,
		equip_name: message.data.equip_name,
		equip_internal_no: message.data.equip_internal_no,
		fixed_assets_type: message.data.fixed_assets_type,
		equipment_state: message.data.equipment_state,
		transfer_time: message.data.transfer_time,
		transfer_price: message.data.transfer_price,
		transfer_date: message.data.transfer_date,
		// geom: message.data.address,
		productid
	});

	if (message.hidden_info && message.hidden_info.length > 0) {
		message.hidden_info.forEach((v) => {
			insert('equip_hazard_source', {
				equip_no: res.result,
				hidden_source: v.hidden_source,
				productid
			});
		});
	}
	// if (message.hidden_data && message.hidden_data.length > 0) {
	// 	message.hidden_data.forEach((t) => {
	// 		insert('hazard_source', {
	// 			hidden_source: t.hidden_source,
	// 			hidden_name: t.hidden_name,
	// 			assess_level: t.assess_level,
	// 			productid
	// 		});
	// 	});
	// }
	if (message.design_data && message.design_data.length > 0) {
		message.design_data.forEach((j) => {
			insert('equ_user_defined_attribute', {
				equip_no: res.result,
				attr_no: j.attr_no,
				attr_name: j.attr_name,
				attr_value: j.attr_value,
				productid
			});
		});
	}
	const r = await exec();
	log('Service end path:e:\考核-2\kh\src\device-management-ygh\zj-000009\s010,action_id:' + action_id);
	return {
		data: r
	};
}
